package com.qo.qsdk.ui.base;

import android.app.Activity;
import android.graphics.Color;
import android.text.InputType;
import android.text.SpannableString;
import android.text.TextPaint;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.LinkMovementMethod;
import android.text.method.PasswordTransformationMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.qo.qsdk.model.SDKInfoModel;
import com.qo.qsdk.model.UserModel;
import com.qo.qsdk.ui.QSDKListener;
import com.qo.qsdk.utils.DynamicUtils;
import com.qo.qsdk.utils.Language;
import com.qo.qsdk.utils.QSDKHelper;

import static android.text.Spanned.SPAN_INCLUSIVE_EXCLUSIVE;

public class LoginView implements IBaseView{

    private QBTextView  mTab1, mTab2, mGuest;
    private QTextView  mAccountDesc, mAccountDesc2, mPwdDesc, mPwdDesc2, mDesc;
    private View mTab1Line, mTab2Line, mPwdVisible, mAgree, mApple, mFb, mGoogle, mThird;

    private EditText mAccountEdit, mPwdEdit;

    private QBTextView mBtn;
    private View mMain;
    private Activity mActivity;

    private QSDKListener mIAccountListener;

    private boolean isLogin;
    private boolean isAgree;

    public LoginView(Activity mActivity, QSDKListener iAccountListener) {
        this.mIAccountListener = iAccountListener;
        this.mActivity = mActivity;
        init(mActivity);
    }

    private void init(Activity v) {
        mMain = v.findViewById(DynamicUtils.id(mActivity, "mainLogin"));
        mMain.setVisibility(View.GONE);

        mTab1 = v.findViewById(DynamicUtils.id(mActivity, "tab1"));
        mTab2 = v.findViewById(DynamicUtils.id(mActivity, "tab2"));
        mTab1Line = v.findViewById(DynamicUtils.id(mActivity, "tab1Line"));
        mTab2Line = v.findViewById(DynamicUtils.id(mActivity, "tab2Line"));
        mBtn = v.findViewById(DynamicUtils.id(mActivity, "loginBtn"));
        mBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v2) {
                if (!isAgree) {
                    Toast.makeText(v, Language.Rule_Agree.toString() + "", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (mIAccountListener != null) {
                    if (isLogin) {
                        mIAccountListener.onLogin(mAccountEdit.getText().toString(), mPwdEdit.getText().toString());
                    }else{
                        mIAccountListener.onRegister(mAccountEdit.getText().toString(), mPwdEdit.getText().toString());
                    }
                }
            }
        });

        mAccountDesc = v.findViewById(DynamicUtils.id(mActivity, "accountDesc"));
        mPwdDesc = v.findViewById(DynamicUtils.id(mActivity, "pwdDesc"));
        mAccountDesc2 = v.findViewById(DynamicUtils.id(mActivity, "accountDesc2"));
        mPwdDesc2 = v.findViewById(DynamicUtils.id(mActivity, "pwdDesc2"));

        mAccountEdit = v.findViewById(DynamicUtils.id(mActivity, "accountEdit"));
        mPwdEdit = v.findViewById(DynamicUtils.id(mActivity, "passEdit"));

        mDesc = v.findViewById(DynamicUtils.id(mActivity, "loginDesc"));
        mAgree = v.findViewById(DynamicUtils.id(mActivity, "loginAgree"));
        mAgree.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                v.setActivated(!v.isActivated());
                isAgree = v.isActivated();
            }
        });
        mPwdVisible = v.findViewById(DynamicUtils.id(mActivity, "loginPwdV"));
        mPwdVisible.setActivated(false);
        mPwdVisible.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                v.setActivated(!v.isActivated());
                if (v.isActivated()) {
                    mPwdEdit.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    mPwdEdit.setSelection(mPwdEdit.getText().length());
                }else{
                    mPwdEdit.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    mPwdEdit.setSelection(mPwdEdit.getText().length());
                }
            }
        });


        mThird = v.findViewById(DynamicUtils.id(mActivity, "third"));
        mFb = v.findViewById(DynamicUtils.id(mActivity, "fb"));
        mFb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v2) {
                if (!isAgree) {
                    Toast.makeText(v, Language.Rule_Agree.toString() + "", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (mIAccountListener != null ) mIAccountListener.onLoginFb();
            }
        });
        mApple = v.findViewById(DynamicUtils.id(mActivity, "apple"));
        mApple.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v2) {
                if (!isAgree) {
                    Toast.makeText(v, Language.Rule_Agree.toString() + "", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (mIAccountListener != null ) mIAccountListener.onLoginApple();
            }
        });
        mGoogle = v.findViewById(DynamicUtils.id(mActivity, "gp"));
        mGoogle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v2) {
                if (!isAgree) {
                    Toast.makeText(v, Language.Rule_Agree.toString() + "", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (mIAccountListener != null ) mIAccountListener.onLoginGoogle();
            }
        });
        mGuest = v.findViewById(DynamicUtils.id(mActivity, "guest"));
        mGuest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v2) {
                if (!isAgree) {
                    Toast.makeText(v, Language.Rule_Agree.toString() + "", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (mIAccountListener != null ) mIAccountListener.onLoginGuest();
            }
        });

        mMain.setVisibility(View.GONE);

        mTab1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changeTab(true);
            }
        });

        mTab2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changeTab(false);
            }
        });

        isLogin = true;
        isAgree = true;
        mAgree.setActivated(isAgree);

        refresh();;
    }

    public void changeTab(boolean isLogin) {
        if (this.isLogin == isLogin) return;
        this.isLogin = isLogin;
        refresh();
        mAccountEdit.setText("");
        mPwdEdit.setText("");
    }

    public void show(Activity activity) {
        changeTab(true);
        mMain.setVisibility(View.VISIBLE);
    }

    public void setAccount(String account) {
        mAccountEdit.setText(account);
    }

    public void setPass(String pass) {
        mPwdEdit.setText(pass);
    }

    public void showLogin(Activity activity) {
        changeTab(true);
        mMain.setVisibility(View.VISIBLE);
    }

    public void showReg(Activity activity) {
        changeTab(false);
        mMain.setVisibility(View.VISIBLE);
    }

    public void hide(Activity activity) {
        mMain.setVisibility(View.GONE);
    }

    public void refresh() {
        UserModel userModel = QSDKHelper.getInstance().getUserInfoModel(this.mActivity);
        SDKInfoModel config = QSDKHelper.getInstance().getConfigureModel();

        mTab1.setText(Language.Login_Tab1.toString());
        mTab2.setText(Language.Login_Tab2.toString());

        mTab1.setActivated(isLogin);
        mTab1Line.setVisibility(isLogin ? View.VISIBLE : View.INVISIBLE);
        mTab2.setActivated(!isLogin);
        mTab2Line.setVisibility(!isLogin ? View.VISIBLE : View.INVISIBLE);

        mAccountDesc.setText(Language.Login_Account.toString());
        mPwdDesc.setText(Language.Login_Pwd.toString());
        mAccountDesc2.setText(Language.Login_Account.toString());
        mPwdDesc2.setText(Language.Login_Pwd.toString());

        mAccountEdit.setHint(Language.Login_Account_Tip.toString());
        mPwdEdit.setHint(isLogin ? Language.Login_Pwd_Tip.toString() : Language.Login_Pwd_Reg_Tip.toString());

        mBtn.setText(isLogin ? Language.Login_Btn_Login.toString() : Language.Login_Btn_Reg.toString());

        mApple.setVisibility(config.hasApple() ? View.VISIBLE : View.GONE);
        mFb.setVisibility(config.hasFacebook() ? View.VISIBLE : View.GONE);
        mGoogle.setVisibility(config.hasGoogle() ? View.VISIBLE : View.GONE);
        mGuest.setVisibility(config.hasGuest() ? View.VISIBLE : View.GONE);
        mThird.setVisibility(isLogin ? View.VISIBLE : View.GONE);
        mGuest.setText(Language.Login_Guest.toString());

        String a = Language.Login_Desc_Pro.toString();
        String b = Language.Login_Desc_Pro_1.toString();
        String c = Language.Login_Desc_Pro_2.toString();
        String d = Language.Login_Desc_Pro_3.toString();
        SpannableString spannableString = new SpannableString(a);
        spannableString.setSpan(new ForegroundColorSpan(Color.parseColor("#727785")),0, a.length(), SPAN_INCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(new ForegroundColorSpan(Color.parseColor("#ED6E3D")),a.indexOf(b), a.indexOf(b) + b.length(), SPAN_INCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(new ClickableSpan() {
            @Override
            public void onClick(@NonNull View widget) {
                if (mIAccountListener != null) {
                    mIAccountListener.onTerms();
                }
            }

            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                ds.setColor(Color.parseColor("#ED6E3D"));
                ds.setUnderlineText(false);
                ds.clearShadowLayer();
            }
        },a.indexOf(b), a.indexOf(b) + b.length(), SPAN_INCLUSIVE_EXCLUSIVE);

        spannableString.setSpan(new ForegroundColorSpan(Color.parseColor("#ED6E3D")),  a.indexOf(d), a.indexOf(d) + d.length(), SPAN_INCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(new ClickableSpan() {
            @Override
            public void onClick(@NonNull View widget) {
                if (mIAccountListener != null) {
                    mIAccountListener.onProtocol();
                }
            }

            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                ds.setColor(Color.parseColor("#ED6E3D"));
                ds.setUnderlineText(false);
                ds.clearShadowLayer();
            }
        }, a.indexOf(d), a.indexOf(d) + d.length(), SPAN_INCLUSIVE_EXCLUSIVE);
        mDesc.setMovementMethod(LinkMovementMethod.getInstance());
        mDesc.setText(spannableString);
        mDesc.setHighlightColor(Color.TRANSPARENT);
    }


}
